﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;

namespace CJJCH.BLL
{
    public  class T_INF_News
    {
        private readonly CJJCH.DAL.T_INF_News dal = new CJJCH.DAL.T_INF_News();
        public T_INF_News()
        { }
        #region  成员方法
        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(Guid ID)
        {
            return dal.Exists(ID);
        }

        /// <summary>
        /// 增加一条数据
        /// </summary>
        public string Add(CJJCH.Model.T_INF_News model)
        {
            int res = 0;
            try
            {
                res = dal.Add(model);
            }
            catch { }
            return res > 0 ? "" : "添加失败：请联系管理员！";
        }

        /// <summary>
        /// 更新一条数据
        /// </summary>
        public void Update(CJJCH.Model.T_INF_News model)
        {
            dal.Update(model);
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public void Delete(Guid ID)
        {

            dal.Delete(ID);
        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public CJJCH.Model.T_INF_News GetModel(Guid ID)
        {

            return dal.GetModel(ID);
        }

        /// <summary>
        /// 得到一个对象实体，从缓存中。
        /// </summary>
        public CJJCH.Model.T_INF_News GetModelByCache(Guid ID)
        {

            string CacheKey = "T_INF_NewsModel-" + ID;
            object objModel = LTP.Common.DataCache.GetCache(CacheKey);
            if (objModel == null)
            {
                try
                {
                    objModel = dal.GetModel(ID);
                    if (objModel != null)
                    {
                        int ModelCache = LTP.Common.ConfigHelper.GetConfigInt("ModelCache");
                        LTP.Common.DataCache.SetCache(CacheKey, objModel, DateTime.Now.AddMinutes(ModelCache), TimeSpan.Zero);
                    }
                }
                catch { }
            }
            return (CJJCH.Model.T_INF_News)objModel;
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            return dal.GetList(strWhere);
        }
        /// <summary>
        /// 获得前几行数据
        /// </summary>
        public DataSet GetList(int Top, string strWhere, string filedOrder)
        {
            return dal.GetList(Top, strWhere, filedOrder);
        }
        /// <summary>
        /// 获得数据列表
        /// </summary>
        public List<CJJCH.Model.T_INF_News> GetModelList(string strWhere)
        {
            DataSet ds = dal.GetList(strWhere);
            return DataTableToList(ds.Tables[0]);
        }
        /// <summary>
        /// 获得数据列表
        /// </summary>
        public List<CJJCH.Model.T_INF_News> DataTableToList(DataTable dt)
        {
            List<CJJCH.Model.T_INF_News> modelList = new List<CJJCH.Model.T_INF_News>();
            int rowsCount = dt.Rows.Count;
            if (rowsCount > 0)
            {
                CJJCH.Model.T_INF_News model;
                for (int n = 0; n < rowsCount; n++)
                {
                    model = new CJJCH.Model.T_INF_News();
                    if (dt.Rows[n]["ID"].ToString() != "")
                    {
                        model.ID = new Guid(dt.Rows[n]["ID"].ToString());
                    }
                    model.NewsTitle = dt.Rows[n]["NewsTitle"].ToString();
                    model.NewsSource = dt.Rows[n]["NewsSource"].ToString();
                    model.NewsNote = dt.Rows[n]["NewsNote"].ToString();
                    if (dt.Rows[n]["TypeID"].ToString() != "")
                    {
                        model.TypeID = int.Parse(dt.Rows[n]["TypeID"].ToString());
                    }
                    if (dt.Rows[n]["IsHot"].ToString() != "")
                    {
                        if ((dt.Rows[n]["IsHot"].ToString() == "1") || (dt.Rows[n]["IsHot"].ToString().ToLower() == "true"))
                        {
                            model.IsHot = true;
                        }
                        else
                        {
                            model.IsHot = false;
                        }
                    }
                    if (dt.Rows[n]["IsTop"].ToString() != "")
                    {
                        if ((dt.Rows[n]["IsTop"].ToString() == "1") || (dt.Rows[n]["IsTop"].ToString().ToLower() == "true"))
                        {
                            model.IsTop = true;
                        }
                        else
                        {
                            model.IsTop = false;
                        }
                    }
                    if (dt.Rows[n]["Hits"].ToString() != "")
                    {
                        model.Hits = int.Parse(dt.Rows[n]["Hits"].ToString());
                    }
                    if (dt.Rows[n]["PlusDate"].ToString() != "")
                    {
                        model.PlusDate = DateTime.Parse(dt.Rows[n]["PlusDate"].ToString());
                    }
                    if (dt.Rows[n]["OrderNum"].ToString() != "")
                    {
                        model.OrderNum = int.Parse(dt.Rows[n]["OrderNum"].ToString());
                    }
                    if (dt.Rows[n]["ManagerID"].ToString() != "")
                    {
                        model.ManagerID = new Guid(dt.Rows[n]["ManagerID"].ToString());
                    }
                    if (dt.Rows[n]["BuildDate"].ToString() != "")
                    {
                        model.BuildDate = DateTime.Parse(dt.Rows[n]["BuildDate"].ToString());
                    }
                    modelList.Add(model);
                }
            }
            return modelList;
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetAllList()
        {
            return GetList("");
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        //public DataSet GetList(int PageSize,int PageIndex,string strWhere)
        //{
        //return dal.GetList(PageSize,PageIndex,strWhere);
        //}

        #endregion  成员方法
        /// <summary>
        /// 首页图书资讯
        /// </summary>
        /// <returns></returns>
        public IList<CJJCH.Model.T_INF_News> GetNewsAgo()
        {
            DataSet ds = dal.GetNewsAgo();
            return ToList(ds.Tables[0]);

        }

        public IList<CJJCH.Model.T_INF_News> ToList(DataTable dt)
        {
            IList<CJJCH.Model.T_INF_News> list = new List<CJJCH.Model.T_INF_News>();
            int rowCount = dt.Rows.Count;
            if (rowCount > 0)
            {
                CJJCH.Model.T_INF_News model;
                for (int i = 0; i < rowCount; i++)
                {
                    model = new Model.T_INF_News();
                    try
                    {
                        model.ID = new Guid(dt.Rows[i]["ID"].ToString());
                    }
                    catch { }
                    try
                    {
                        model.BuildDate = DateTime.Parse(dt.Rows[i]["BuildDate"].ToString());
                    }
                    catch { }
                    try
                    {
                        model.Hits = int.Parse(dt.Rows[i]["Hits"].ToString());
                    }
                    catch { }
                    try
                    {
                        if (dt.Rows[i]["IsHot"].ToString().ToLower() == "true" || dt.Rows[i]["IsHot"].ToString() == "1")
                        {
                            model.IsHot = true;
                        }
                        else
                        {
                            model.IsHot = false;
                        }
                    }
                    catch { }
                    try
                    {
                        if (dt.Rows[i]["IsTop"].ToString() == "1" || dt.Rows[i]["IsTop"].ToString() == "true")
                        {
                            model.IsTop = true;
                        }
                        else
                        {
                            model.IsTop = false;
                        }
                    }
                    catch { }
                    try
                    {
                        model.ManagerID = new Guid(dt.Rows[i]["ManagerID"].ToString());
                    }
                    catch { }
                    try
                    {
                        model.NewsNote = dt.Rows[i]["NewsNote"].ToString();
                    }
                    catch { }
                    try
                    {
                        model.NewsSource = dt.Rows[i]["NewsSource"].ToString();
                    }
                    catch { }
                    try
                    {
                        model.NewsTitle = dt.Rows[i]["NewsTitle"].ToString();
                    }
                    catch { }
                    try
                    {
                        model.OrderNum = int.Parse(dt.Rows[i]["OrderNum"].ToString());
                    }
                    catch { }
                    try
                    {
                        model.PlusDate = DateTime.Parse(dt.Rows[i]["PlusDate"].ToString());
                    }
                    catch { }
                    try
                    {
                        model.TypeID = int.Parse(dt.Rows[i]["TypeID"].ToString());
                    }
                    catch { }
                    model.ModelType = new Model.T_INF_NewsType();
                    try
                    {
                        model.ModelType.TypeName = dt.Rows[i]["TypeName"].ToString();
                    }
                    catch { }

                    list.Add(model);
                }
            }
            return list;
        }
    }
}
